﻿using BJoin.Application.AppException;
using BJoin.Application.Dto;
using BJoin.Common;
using BJoin.DemoProject.Dtos;
using BJoin.DemoProject.Entities;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BJoin.DemoProject.Manager
{
    public partial class FileManager
    {
        //private IFilePageRepository filePageRepository;
        partial void Init()
        {
            //init other repository
            //filePageRepository = ServiceProvider.GetService<IFilePageRepository>() ?? throw new Exception($"{nameof(IFilePageRepository)} init fail");
        }

        #region BizValidater
        protected override async Task BizValidaterAsync<FileAddDto>(FileAddDto addDto)
        {
            var entity = Mapper.Map<FileEntity>(addDto);
            await ValidationDisplay(new List<FileEntity> { entity }, exists =>
            {
                if (exists.IsAny())
                    return MessageEnum.ExistCannotDuplicate;
                return MessageEnum.OK;
            }, true);
        }
        protected override async Task BizValidaterAsync<FileAddDto>(IEnumerable<FileAddDto> addDtos)
        {
            var entities = Mapper.Map<IEnumerable<FileEntity>>(addDtos);
            await ValidationDisplay(entities, exists =>
            {
                if (exists.IsAny())
                    return MessageEnum.ExistCannotDuplicate;
                return MessageEnum.OK;
            }, true);
        }
        protected override async Task BizValidaterAsync<FileUpdateDto>(long id, FileUpdateDto updateDto)
        {
            var entity = Mapper.Map<FileEntity>(updateDto);
            await ValidationDisplay(new List<FileEntity> { entity }, exists =>
            {
                if (exists.IsAny(es => es.Id != id))
                    return MessageEnum.ExistCannotDuplicate;
                return MessageEnum.OK;
            }, string.IsNullOrWhiteSpace(entity.Code));
        }
        private async Task ValidationDisplay(IEnumerable<FileEntity> entities, Func<IEnumerable<FileEntity>, MessageEnum> func, bool isValidation)
        {
            if (isValidation && entities.IsAny())
            {
                var exists = await WriteAsync(async database => await Repository.GetListAsync(database,new FileFilterDto
                {
                    //不能重复字段
                    Code = entities.Select(e => e.Code).ToArray()
                }));
                if (exists.IsAny())
                {
                    switch (func(exists))
                    {
                        case MessageEnum.ExistCannotDuplicate:
                            throw new HttpStatusCodeException(StatusCodes.Status400BadRequest,
                               string.Format(EnumHelper.GetEnumDescription<MessageEnum>(MessageEnum.ExistCannotDuplicate), nameof(FileEntity.Code)));
                    }
                }
            }

        }
        #endregion

        public async Task<FileDto> GetFileWithFormatDesc(long id)
        {
            var result = await GetFileWithChild();
            return await GetSingleAsync(id, async f =>
            {
                var list = await FilterAsync(f => f.Id == 736151116688916480);
                f.FileFormatDesc = EnumHelper.GetEnumDescription(f.FileFormat);
            });
        }
        public async Task<ResPagingDto<FileDto>> GetFileTranslater()
        {
            var list = await PagingFilterAsync(new FileFilterDto { }, 0, 1, async data =>
            {
                var file = await GetSingleAsync(1);
                data.ForEach(d =>
                {
                    d.FileFormatDesc = null == file ? string.Empty : EnumHelper.GetEnumDescription(d.FileFormat);
                });
            });
            return list;
        }

        private async Task<ResPagingDto<FileWithChildDto>> GetFileWithChild()
        {
            var pagingFiles = await PagingFilterAsync(new FileFilterDto { },0,1);
            var pagingFxDto = Mapper.Map<ResPagingDto<FileWithChildDto>>(pagingFiles);
            pagingFxDto.Items.AsList().ForEach(fx =>
            {
                fx.ChildFiles = new List<FileDto>();
            });
            return pagingFxDto;
        }
        public Task<(string, byte[])> DownloadAsync(long id)
        {
            throw new NotImplementedException();
        }

        public Task<IList<string>> UploadAsync(IList<IFormFile> files)
        {
            throw new NotImplementedException();
        }
    }
}
